# -*- coding=utf-8-*-
"""
@File:memory_selection
@Author:Created by Han X.Y
@Date:on 2021/8/6 15:52 
"""


# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution:
    def isSymmetric(self, root):
        def isSymPQ(p, q):
            if not q and not p:
                return True
            elif not q or not p:
                return False
            else:
                return p.val == q.val and isSymPQ(p.left, q.right) and isSymPQ(p.right, q.left)

        if not root:
            return True
        return isSymPQ(root.left, root.right)